<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>V字型升级版</title>
    <style>
        html,
        body,
        ul,
        li {
            margin: 0;
            padding: 0;
        }
        
        div {
            margin: 0;
            padding: 20px;
        }
        
        #squareList {
            position: relative;
            margin-top: 30px;
            width: 600px;
            height: 400px;
        }
        
        li {
            list-style-type: none;
        }
        
        .square {
            position: absolute;
            width: 50px;
            height: 50px;
            background-color: red;
            font-size: 30px;
            line-height: 50px;
            text-align: center;
            color: #000;
        }
    </style>
</head>

<body>
    <div>
        <button id="btn">自动生成一个V字型</button>
        <ul id="squareList">
        </ul>
    </div>

    <script>
        var btn = document.getElementById('btn');
        var list = document.getElementById('squareList');
        var sum = 0;
        btn.onclick = function() {
            list.innerHTML = '';
            switch (sum % 4) {
                case 0:
                    {
                        var poiLeft = -50;
                        var poiTop = 150;
                        for (var i = 0; i < 5; i++) {
                            poiLeft += 50;
                            if (i < 3) {
                                poiTop -= 50;
                            } else {
                                poiTop += 50;
                            }
                            list.innerHTML += '<li class="square" style="left: ' + poiLeft + 'px; top: ' + poiTop + 'px;">' + i + '</li>'
                        }
                        break;
                    }
                case 1:
                    {
                        var poiLeft = -50;
                        var poiTop = -50;
                        for (var i = 0; i < 5; i++) {
                            poiTop += 50;
                            if (i < 3) {
                                poiLeft += 50;
                            } else {
                                poiLeft -= 50;
                            }
                            list.innerHTML += '<li class="square" style="left: ' + poiLeft + 'px; top: ' + poiTop + 'px;">' + i + '</li>'
                        }
                        break;
                    }
                case 2:
                    {
                        var poiLeft = -50;
                        var poiTop = -50;
                        for (var i = 0; i < 5; i++) {
                            poiLeft += 50;
                            if (i < 3) {
                                poiTop += 50;
                            } else {
                                poiTop -= 50;
                            }
                            list.innerHTML += '<li class="square" style="left: ' + poiLeft + 'px; top: ' + poiTop + 'px;">' + i + '</li>'
                        }
                        break;
                    }
                case 3:
                    {
                        var poiLeft = 150;
                        var poiTop = -50;
                        for (var i = 0; i < 5; i++) {
                            poiTop += 50;
                            if (i < 3) {
                                poiLeft -= 50;
                            } else {
                                poiLeft += 50;
                            }
                            list.innerHTML += '<li class="square" style="left: ' + poiLeft + 'px; top: ' + poiTop + 'px;">' + i + '</li>'
                        }
                        break;
                    }
                default:
                    break;
            }
            sum++;
        };
    </script>
</body>

</html>